﻿using System;
using System.Collections;
using System.Configuration;
using System.Data;
using System.Web;
using System.Web.Security;
using System.Web.UI;
using System.Web.UI.HtmlControls;
using System.Web.UI.WebControls;
using System.Web.UI.WebControls.WebParts;
using System.Data.SqlClient;
using DataAccessEntLib;
using DAL;
public partial class list : System.Web.UI.Page
{ 
    AdDB _objAdDb = new AdDB();
    RegistrationData _objRegistrationData = new RegistrationData();
            int AdSubCategoryID = 0;
            string ViewType = string.Empty;

    protected void Page_Load(object sender, EventArgs e)
    {
        adList1.RequestDataSet += new Classified_UserControls_Cld_List.ReBindDataSet(UC_RequestDataSet);

        Label lblEmpty = adList1.FindControl("lblEmptyMessage") as Label;
        if (!IsPostBack)
        {
            if (!object.Equals(Request.QueryString["ViewType"], null))
            {
                if (Convert.ToString(Request.QueryString["ViewType"]) != string.Empty)
                {
                    ViewType = Convert.ToString(Request.QueryString["ViewType"]);
                    if (ViewType == "MyAds")
                    {
                        if (!object.Equals(Session["User"], null))
                        {
                            _objRegistrationData = Session["User"] as RegistrationData;
                            adList1.dsAds = BindAdList(AdSubCategoryID, _objRegistrationData.UserID, ViewType);
                            lblSubCatName.Text = "My Ads";
                            lblEmpty.Text = "Sorry you have not posted any Ad. To post an Ad <a href='/classified/Cld-Post-Ad.aspx'>Click Here</a>";

                        }
                        else
                        {
                            Response.Redirect(Common.LogOutPage);
                        }
                    }
                }
            }
            else if (!object.Equals(Request.QueryString["SubCatID"], null))
            {
                if (Convert.ToString(Request.QueryString["SubCatID"]) != string.Empty)
                {
                    AdSubCategoryID = Convert.ToInt32(Request.QueryString["SubCatID"]);
                    adList1.dsAds = BindAdList(AdSubCategoryID, 0, "SubCat");
                }
            }
            else
            {
                lblSubCatName.Text = "All Ads";
                adList1.dsAds = BindAdList(AdSubCategoryID);
            }
        }


    }

    protected DataSet BindAdList(int AdSubCategoryID, int ID = 0, string ViewType = "")
    {
        DataSet dsAds = new DataSet();

        dsAds = _objAdDb.Select_AdMaster(0, ViewType, ID, false, AdSubCategoryID);
        if (dsAds != null)
        {
            if (dsAds.Tables.Count > 0)
            {
                if (dsAds.Tables[0].Rows.Count > 0)
                    if (ViewType == "SubCat")
                        lblSubCatName.Text = Convert.ToString(dsAds.Tables[0].Rows[0]["SubCategory"]);
            }
        }
        return dsAds;

    }

    DataSet UC_RequestDataSet(object sender)
    {
        if (!object.Equals(Request.QueryString["ViewType"], null))
        {
            if (Convert.ToString(Request.QueryString["ViewType"]) != string.Empty)
            {
                ViewType = Convert.ToString(Request.QueryString["ViewType"]);
                if (ViewType == "MyAds")
                {
                    if (!object.Equals(Session["User"], null))
                    {
                        _objRegistrationData = Session["User"] as RegistrationData;
                        return  BindAdList(AdSubCategoryID, _objRegistrationData.UserID, ViewType);

                    } 
                }
            }
        }
        else if (!object.Equals(Request.QueryString["SubCatID"], null))
        {
            if (Convert.ToString(Request.QueryString["SubCatID"]) != string.Empty)
            {
                AdSubCategoryID = Convert.ToInt32(Request.QueryString["SubCatID"]);
                return  BindAdList(AdSubCategoryID, 0, "SubCat");
            }
        }
        else
        {
            lblSubCatName.Text = "All Ads";
           return  BindAdList(AdSubCategoryID);
        }
        return null;
    }
     
}